<?php
// +----------------------------------------------------------------------
// | Description: 用户
// +----------------------------------------------------------------------
// | Author: linchuangbin <linchuangbin@honraytech.com>
// +----------------------------------------------------------------------

namespace app\admin\model;

use think\Db;
use app\admin\model\Common;
use com\verify\HonrayVerify;

class User extends Common
{
    public function login($param){
        $username=$param['username'];
        $password=user_md5($param['password']);
        $res=Db::table('oa_admin_user')
            ->where('username',$username)
            ->where('password',$password)
            ->select();
        if($res){
            if($res[0]['status']!=1){
                $this->error='账号被禁用';
                return false;
            }else{
                return true;
            }
        }else{
            $this->error='账号或者密码错误';
            return false;
        }
    }

    public function getmenu(){
        $data1=Db::table('oa_admin_menu')
              ->where('status',1)
              ->where('pid',0)
              ->select();
        for($i=0;$i<count($data1);$i++){
            $children=Db::table('oa_admin_menu')
                    ->where('status',1)
                    ->where('pid',$data1[$i]['id'])
                    ->select();
            if($children){
                $data1[$i]['children']=$children;
            }
        }
        return $data1;
    }

    public function userWrite($param){
        $info['username']=$param['username'];
        $info['realname']=$param['username'];
        $info['password']=user_md5($param['password']);
        $info['email']=$param['email'];
        $info['phone']=$param['phone'];
        $info['level']=$param['level'];
        $info['status']=1;
        $info['create_time']=date("Y-m-d h:m:s");
        $info['update_time']=date("Y-m-d h:m:s");

        $res=Db::table('oa_admin_user')
            ->where('username',$info['username'])
            ->select();        
        if($res){
            $this->error='用户名已存在';
            return false;
        }else{
            $res1=Db::table('oa_admin_user')
            ->insert($info);
           if($res1){
               return true;
           }else{
               $this->error='添加失败';
               return false;
           }
        }
    }

    public function userEdit($param){
        $info['id']=$param['id'];
        $info['email']=$param['email'];
        $info['phone']=$param['phone'];
        $info['level']=$param['level'];
        $info['status']=$param['status'];
        $info['update_time']=date("Y-m-d H:i:s");

        $res=Db::table('oa_admin_user')
            ->where('id',$info['id'])
            ->update([
                'email'=>$info['email'],
                'phone'=>$info['phone'],
                'level'=>$info['level'],
                'status'=>$info['status'],
                'update_time'=>$info['update_time']
            ]);   
        if($res){
            return true;
        }else{
            $this->error='修改失败';
            return false;
        }
    }

    public function userDelete($param){
        $id=$param['id'];
        $res=Db::table('oa_admin_user')
            ->where('id',$id)
            ->delete();
        if($res){
                return true;
            }else{
                $this->error='删除失败';
                return false;
            }   
    }

    public function getuser($param){
        $map=[];
        $map['realname']=['like', '%'.$param['params']['query'].'%'];
        $data=Db::table('oa_admin_user')
              ->where($map)
              ->field('id,realname,email,phone,level,status')
              ->paginate(config('pagesize'), false, ['page' => $page]);
         return $data; 
    }
    public function getuserbyId($param){
        $data=Db::table('oa_admin_user')
              ->where('id',$param['id'])
              ->field('id,username,email,phone,level,status')
              ->find();
         return $data; 
    }
}
